#include "wnavigate.h"
#include "ui_wnavigate.h"
#include <QPushButton>
#include <QLabel>

WNavigate::WNavigate(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WNavigate)
{
    ui->setupUi(this);
    ui->bNext->setEnabled(false);
    ui->bPrevious->setEnabled(false);
}

WNavigate::~WNavigate()
{
    delete ui;
}

void WNavigate::add(WTable *table)
{
    lTablePrev.append(table);
    foreach (WTable *table, lTableNext) {
        delete table;
    }
    lTableNext.clear();
    ui->bNext->setEnabled(false);
    if(lTablePrev.size() > 1)
        ui->bPrevious->setEnabled(true);
    setLabel();
}

void WNavigate::on_bPrevious_clicked()
{
    WTable * prev = lTablePrev.pop();
    lTableNext.append(prev);
    if(lTablePrev.size() <= 1)
        ui->bPrevious->setEnabled(false);
    ui->bNext->setEnabled(true);
    setLabel();
    emit navigation(lTablePrev.top());
}

void WNavigate::on_bNext_clicked()
{
    WTable * n = lTableNext.pop();
    lTablePrev.append(n);
    if(lTableNext.isEmpty())
        ui->bNext->setEnabled(false);
    if(lTablePrev.size() > 1)
        ui->bPrevious->setEnabled(true);
    setLabel();
    emit navigation(n);
}

void WNavigate::setLabel()
{
    QString label;
    if(lTablePrev.size() <= 1)
    {
        label = QObject::tr("Library");
    } else {
        for(QStack<WTable *>::const_iterator it = lTablePrev.constBegin();
            it != lTablePrev.constEnd() - 1; it ++)
            label += (*it)->getPath() + "/";
        label = label.left(label.size() - 1);
    }
    ui->labelPath->setText(label);
}
